package sn.gandal.mobile.kalpet.data.entities;

import java.sql.SQLException;

public class DataBaseCrudHelper {
	
	
    private DatabaseHelper helper;
    
    public DataBaseCrudHelper(DatabaseHelper helper ){
    	this.helper=helper;    	
    }

    public int save(Object obj){
    	int retour=-1;
    	if (obj instanceof Account) {
    		Account account= (Account)obj;
    		retour=createAccount(account);				
		}else if(obj instanceof Credentials) {
			Credentials credential= (Credentials)obj;
    		retour=createCredential(credential);
		}
    	
    	return retour;
    }
    
    private int createAccount(Account account){
    	int value= -1;
    	try {
			value=getHelper().getAccountDao().createIfNotExists(account).getId();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    	return value;
    }
    
    private int createCredential(Credentials credential){
    	int value= -1;
    	try {
			value=getHelper().getCredentialsDao().createIfNotExists(credential).getId();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    	return value;
    }

	
    
    
    
    public DatabaseHelper getHelper() {
		return helper;
	}

	public void setHelper(DatabaseHelper helper) {
		this.helper = helper;
	}
    
    
}
